//
//  BC_Game.m
//  CocOth
//
//  Created by BrunoCausse on 02/12/10.
//  Copyright 2010-2013 poulailler cie. All rights reserved.
//

#import "BC_Game.h"

#import "BC_BrowserTreeNode.h"
#import "BC_Board.h"

NSString * const BCEventKey	= @"event";
NSString * const BCSiteKey	= @"site";
NSString * const BCDateKey	= @"date";
NSString * const BCRoundKey	= @"round";

NSString * const BCRootPositionKey	= @"rootPosition";
NSString * const BCBlackKey			= @"blackPlayer";
NSString * const BCWhiteKey			= @"whitePlayer";
NSString * const BCResultKey		= @"result";

//NSString * const BCRootKey			= @"root";


@implementation BC_Game

@synthesize event, site, round, date;
@synthesize rootPosition, blackPlayer, whitePlayer, result;
@synthesize root;

+ (BC_Game *)gameWithPropertyList:(NSDictionary *)dictionary
{
	BC_Game *game = [[BC_Game alloc] init];
	
	[game setEvent:[dictionary valueForKey:BCEventKey]];
	[game setSite:[dictionary valueForKey:BCSiteKey]];
	[game setDate:[dictionary valueForKey:BCDateKey]];
	[game setRound:[dictionary valueForKey:BCRoundKey]];
	
	[game setRootPosition:[dictionary valueForKey:BCRootPositionKey]];
	
	[game setBlackPlayer:[dictionary valueForKey:BCBlackKey]];
	[game setWhitePlayer:[dictionary valueForKey:BCWhiteKey]];
	
	[game setResult:[dictionary valueForKey:BCResultKey]];
	
	BC_Board *board = [[BC_Board alloc] initWithString:[game rootPosition]];
	
	[game setRoot:[BC_BrowserTreeNode nodeWithPropertyList:[dictionary valueForKey:BCRootKey] withBoard:board]];
	
	[board release];
	
	return [game autorelease];
	
}

- (NSDictionary *)propertyList
{
	NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithCapacity:9];

	//head
	[dictionary setObject:[self event] forKey:BCEventKey];
	[dictionary setObject:[self site] forKey:BCSiteKey];
	[dictionary setObject:[self date] forKey:BCDateKey];
	[dictionary setObject:[self round] forKey:BCRoundKey];
	
	[dictionary setObject:[self rootPosition] forKey:BCRootPositionKey];
	[dictionary setObject:[self blackPlayer] forKey:BCBlackKey];
	[dictionary setObject:[self whitePlayer] forKey:BCWhiteKey];
	[dictionary setObject:[self result] forKey:BCResultKey];

	[dictionary setObject:[[self root] propertyList] forKey:BCRootKey];
	
	return dictionary;
	
}



- (id)init
{
	return [self initWithPosition:@"[-------- -------- -------- ---OX--- ---XO--- -------- -------- -------- X]"];
}

- (id)initWithPosition:(NSString *)boardString
{
	self = [super init];
	if (self) {
		event = @"??";
		site = @"??";
		round = @"??";
		date = @"dd/mm/yyyy";
		
		
		[self setRootPosition:boardString];
		blackPlayer = @"??";
		whitePlayer = @"??";
		result = @"bb-ww";
		
		root = [[BC_BrowserTreeNode treeNodeEmpty] retain];
		
	}
	
	return self;
}

- (void)dealloc
{
	[event release];
	[site release];
	[round release];
	[date release];
	[rootPosition release];
	[blackPlayer release];
	[whitePlayer release];
	[result release];
	[root release];
	
	[super dealloc];
}


@end
